package com.liu.jsoup.demo.HtmlParseUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;

/**
 * @Author QS.Liu
 * @date 2020/6/5/8:39
 */
public class HtmlParseUtil {


    public static void main(String[] args) {
        getImages("https://search.jd.com/Search?keyword=java");
    }

    public static void getImages(String url){
        //前提，需要联网
        try {
            Document document = Jsoup.parse(new URL(url), 3000);
            Element j_goodsList = document.getElementById("J_goodsList");
            Elements li = j_goodsList.getElementsByTag("li");
            for (Element element : li) {
                String img = element.getElementsByTag("img").eq(0).attr("src");
                String price = element.getElementsByClass("p-price").eq(0).text();
                String name = element.getElementsByClass("p-name").eq(0).text();
                System.out.println(price + "--"+name);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
